﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using System.Collections.Generic;
using THE_WIND_RISES.Common.Domans.Produce.Craft;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Technology
{
    public class ProcessRouteCreateHandler : IRequestHandler<ProcessRouteCreateCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        private readonly IBaseRepository<ProcessRoute>processRouterepository;
        private readonly IMapper mapper;
        private readonly IBaseRepository<OperationLine> OperationLinerepository;
        private readonly ILogger<ProcessRouteCreateHandler> logger;
        public ProcessRouteCreateHandler(IBaseRepository<ProcessRoute> processRouterepository, IMapper mapper, IBaseRepository<OperationLine> OperationLinerepository, ILogger<ProcessRouteCreateHandler> logger)
        {
            this.processRouterepository = processRouterepository;
            this.mapper = mapper;
            this.OperationLinerepository = OperationLinerepository;
            this.logger = logger;
        }

       
        public async Task<Result> Handle(ProcessRouteCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new  Result() { Code=(int)CommonErrorCodes.Added};

            try
            {
                string ProcessRouteSql = $"select * from ProcessRoute where ProcessRouteNo='{request.ProcessRouteNo}'";
                var ProcessRouteList = processRouterepository.GetAll(ProcessRouteSql).FirstOrDefault();
                if (ProcessRouteList != null)
                {
                    result.Code = (int)CommonErrorCodes.Added;
                    result.Data = null;
                    result.Msg = "编号重复 请重新输入";
                    logger.LogError("添加失败 编号重复 请重新输入");
                   foreach (var item in request.OperationLineList)
                    {
                        var OperationLineSql = "insert into OperationLine values(@OperationLineId,@SerialNumber,@ProcessId,@WorkCenterId,@LeadTime,@LabourPrice,@LostButton,@Abstract,@OperationStatus,@ProcessRouteId,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid)";
                        string ProcessRouteSq2 = $"select * from ProcessRoute where ProcessRouteId={ProcessRouteList.ProcessRouteId}";
                        var AddNum = OperationLinerepository.Add(OperationLineSql, new
                        {
                            OperationLineId = 0,
                            SerialNumber = item.SerialNumber,
                            ProcessId = item.ProcessId,
                            WorkCenterId = item.WorkCenterId,
                            LeadTime = item.LeadTime,
                            LabourPrice = item.LabourPrice,
                            LostButton = item.LostButton,
                            Abstract = item.Abstract,
                            OperationStatus = item.OperationStatus,
                            ProcessRouteId = ProcessRouteList.ProcessRouteId,
                            Handle = request.Handle,
                            HandleContext = request.HandleContext,
                            HandleTime = DateTime.Now,
                            Pid = request.Pid,
                            IsDelete = item.IsDelete
                        });
                        
                    }

                   return result;
                }

                else
                {
                    var AddData = mapper.Map<ProcessRoute>(request);
                     ProcessRouteSql = "insert into ProcessRoute values(@ProcessRouteId,@ProcessRouteNo,@ProcessRouteName,@ProcessVersion,@ProcessDescription,@InventoryInfoId,@ProcessRouteStatus,@ProcessRouteApproval,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid)";
                    var AddNum = processRouterepository.Add(ProcessRouteSql, new
                    {
                        ProcessRouteId = AddData.ProcessRouteId,
                        ProcessRouteNo = AddData.ProcessRouteNo,
                        ProcessRouteName = AddData.ProcessRouteName,
                        ProcessVersion = AddData.ProcessVersion,
                        ProcessDescription = AddData.ProcessDescription,
                        InventoryInfoId = request.InventoryInfoId,
                        ProcessRouteStatus = AddData.ProcessRouteStatus,
                        ProcessRouteApproval = AddData.ProcessRouteApproval,
                        Handle = request.Handle,
                        HandleContext = request.HandleContext,
                        HandleTime = DateTime.Now,
                        Pid = request.Pid,
                        IsDelete = AddData.IsDelete
                    });
                    if (AddNum > 0)
                    {
                        string ProcessRouteSql1 = $"select * from ProcessRoute where ProcessRouteNo='{request.ProcessRouteNo}'";
                         ProcessRouteList = processRouterepository.GetAll(ProcessRouteSql1).FirstOrDefault();
                        var data = new
                        {
                            ProcessRouteId = ProcessRouteList.ProcessRouteId,
                        };
                        foreach (var item in request.OperationLineList)
                        {
                            var OperationLineSql = "insert into OperationLine values(@OperationLineId,@SerialNumber,@ProcessId,@WorkCenterId,@LeadTime,@LabourPrice,@LostButton,@Abstract,@OperationStatus,@ProcessRouteId,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid)";
                            string ProcessRouteSq2 = $"select * from ProcessRoute where ProcessRouteId={ProcessRouteList.ProcessRouteId}";
                            var AddDataa= OperationLinerepository.Add(OperationLineSql, new
                            {
                                OperationLineId = 0,
                                SerialNumber = item.SerialNumber,
                                ProcessId = item.ProcessId,
                                WorkCenterId = item.WorkCenterId,
                                LeadTime = item.LeadTime,
                                LabourPrice = item.LabourPrice,
                                LostButton = item.LostButton,
                                Abstract = item.Abstract,
                                OperationStatus = item.OperationStatus,
                                ProcessRouteId = ProcessRouteList.ProcessRouteId,
                                Handle = request.Handle,
                                HandleContext = request.HandleContext,
                                HandleTime = DateTime.Now,
                                Pid = request.Pid,
                                IsDelete = item.IsDelete
                            });

                        }
                        result.Code = (int)CommonErrorCodes.Success;
                        result.Data = null;
                        result.Msg = "添加工艺路线成功";
                        logger.LogInformation("添加工艺路线成功");
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"添加工艺路线失败," + ex.Message);
                throw;
            }

           return result;

        }
    }
}
